<template>
  <div class="modify-psw">
    <!--修改密码-->
    <content-header title="修改密码"></content-header>
    <el-form :model="form" status-icon :rules="rules" ref="form" label-width="100px">
      
      <el-form-item label="旧密码" class="old-pw" prop="oldPassword">
        <el-input type="password" v-model="form.oldPassword" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="password">
        <el-input type="password" v-model="form.password" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="checkPass">
        <el-input type="password" v-model="form.checkPass" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')" v-loading="loading">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import ContentHeader from "@/components/widge/content-header";
import { updatePassword } from "@/api/login";

export default {
  components: { ContentHeader },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.form.checkPass !== "") {
          this.$refs.form.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      form: {
        password: "",
        checkPass: "",
        oldPassword: ""
      },
      rules: {
        // oldPassword: [
        //   { required: true, message: "请输入旧密码", trigger: "blur" }
        // ],
        password: [
          { required: true, message: "请设置密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "用户名长度为6～15之间",
            trigger: "blur"
          },
          {
            pattern: /^(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{6,15}$/,
            message: "密码为数字和英文的组合，不包含特殊字符，6-15位。",
            trigger: "blur"
          }
        ],
        checkPass: [
          { required: true, message: "请再此输入新密码", trigger: "blur" },
          { validator: validatePass2, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      if (!this.form.oldPassword.trim()) {
        this.$message({
          message: "请输入旧密码"
        });
        return false;
      }
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading = true;
          updatePassword(this.form.oldPassword, this.form.password)
            .then(res => {
              this.loading = false;
              if (res.flag == 1) {
                this.$message({
                  showClose: true,
                  type: "success",
                  message: res.msg
                });
                resetForm(formName);
              } else {
                this.$message({
                  showClose: true,
                  type: "error",
                  message: res.msg
                });
                this.$refs[formName].clearValidate();
              }
            })
            .catch(err => {
              this.loading = false;
              this.$refs[formName].clearValidate();
            });
        } else {
          this.$message({
            showClose: true,
            type: "error",
            message: "修改失败，请按照要求填写密码后重试"
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="less">
.modify-psw {
  .el-form {
    width: 538px;
    margin: 35px auto;
  }
  .el-button {
    width: 100%;
  }
  .el-button--primary {
    color: #fff;
    background-color: #dbad23;
    border-color: #dbad23;
  }
  .old-pw .el-form-item__label:before {
    content: "*";
    color: #f56c6c;
    margin-right: 4px;
  }
}
</style>
